﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ModdbFrontEnd frontEnd = new ModdbFrontEnd();
    }
    protected void GridView1_DataBinding(object sender, EventArgs e)
    {
        GridView grid1 = (GridView)sender;

    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        int i = GridView1.Columns.Count;

    }
    protected void TablesDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string table = ((DropDownList)sender).SelectedValue;
        CreateColumnsInGridView(table);
        GridView1.DataBind();
    }
    protected void TablesDropDownList_DataBound(object sender, EventArgs e)
    {
        string table = ((DropDownList)sender).SelectedValue;
        CreateColumnsInGridView(table);
    }

    private void CreateColumnsInGridView(string tabelName)
    {
        ModdbFrontEnd frontEnd = new ModdbFrontEnd();
        DataTable table = frontEnd.GetDataTable(tabelName);
        Moddb08_WCF.Columns columns = frontEnd.GetAllColumns(tabelName);
        GridView1.Columns.Clear();
        
        foreach (DataColumn col in table.Columns)
        {
            Moddb08_WCF.Column column =  columns.Find(c => c.Name == col.ColumnName);
            if (!column.Mime.Equals("text/plain"))
            {
                HyperLinkField hyperLinkField = new HyperLinkField();
                hyperLinkField.HeaderText = col.ColumnName;
                string[] stringCollection = new string[1];
                stringCollection[0] = col.ColumnName;
                hyperLinkField.DataNavigateUrlFields = stringCollection;
                hyperLinkField.DataNavigateUrlFormatString = "{0}"; 
                //hyperLinkField.DataTextField = col.ColumnName;
                //hyperLinkField.DataTextFormatString = "{0}";
                hyperLinkField.Text = "Link";

                GridView1.Columns.Add(hyperLinkField);
            }
            else
            {
                BoundField field = new BoundField();
                field.HeaderText = col.ColumnName;
                field.DataField = col.ColumnName;
                GridView1.Columns.Add(field);
            }
        }
        GridView1.AutoGenerateEditButton = true;
        GridView1.AutoGenerateDeleteButton = true;
        GridView1.DataSource = table;

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        
        string test = TablesDropDownList.SelectedValue;
        CreateColumnsInGridView(test);
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataBind();
        Session["SelectedRowIndex"] = e.NewEditIndex;
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
}
